
package vn.com.aptech.msh.utils;

/**
 *
 * @author trietto
 */

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value="confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException
    {
        String passwordId = (String) component.getAttributes().get("passwordId");
        UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(passwordId);
        String password = (String) passwordInput.getValue();

        String errorRes = (String) component.getAttributes().get("errorRes");

        // Cast the value of the entered password of the second field back to String.
        String confirm = (String) value;

        // Compare the first password with the second password.
        if (!password.equals(confirm)) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get(errorRes), "");
            throw new ValidatorException(message);
        }
    }

}